package com.example.demo.mapper;

import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {
	@Results(id = "studentMap", value = {
			@Result(property = "id", column = "id"),
			@Result(property = "name", column = "name"),
			@Result(property = "number", column = "no"),
			@Result(property = "sex", column = "sex"),
			@Result(property = "phone", column = "phone"),
			@Result(property = "qq", column = "qq")
	})
	@Select("select * from tb_student")
	List<Student> getAll();

	// 通过学号查询学生信息
	@ResultMap("studentMap")
	@Select("select  * from tb_student where no = #{no}")
	List<Student> getStudentByNumber(String no);


    // 添加学生信息
	@Insert("insert into tb_student(no,name,sex,phone,qq) values(#{number},#{name},#{sex},#{phone},#{qq})")
	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, before = false)
    int addStudent(Student student);

	// 通过学号查询学生信息，包含Card的信息
	@Select("select  * from tb_student where no = #{no}")
	@Results(id = "studentMap2", value = {
			@Result(property = "number", column = "no"),
			@Result(property = "card", column = "no",
			       one =@One(select = "com.example.demo.mapper.CardMapper.getCardByCardNo")
			)
	})
    Student getStudentByNumber2(String no);


	// 通过学号或者姓名进行查询
	@ResultMap("studentMap")
//	@Select("select  * from tb_student where no = #{number} or name = #{name}")


	// 使用动态sql来实现
	@Select("<script>select * from tb_student where 1=1 " +
			"<if test='number !=null'>and no = #{number}</if>" +
			"<if test='name !=null'>and name = #{name}</if>" +
			"</script>")
	List<Student> getStudentByNumberOrName(Student student);
}